#!/bin/bash
#  安装相关软件包
yum -y install mariadb mariadb-devel mariadb-server \
               php php-fpm php-mysql &> /dev/null 
if [ `echo $?` -ne 0 ];then
echo -e "\033[5;31m请检查yum仓库是否配置正确！！\033[0m" && exit
fi
systemctl start php-fpm
systemctl start mariadb
ss -antulp | grep 3306 &> /dev/null
if [ $(echo $?) != 0 ];then
echo -e "\033[5;31m请检查mariadb服务！！\033[0m" && exit
fi
ss -antulp | grep php-fpm &> /dev/null
if test `echo $?` -ne 0;then
echo -e "\033[5;31m请检查php-fpm！！\033[0m" && exit
fi
#  创建动态测试页面
echo  "<html>
<body>
This is HTML message
</br>
<?php
\$c = 12;
\$d = 2;
if(\$c > \$d){echo 'c is bigger';}
else{ echo 'd is bigger';}
?>
</body>
</html>" > /usr/local/nginx/html/test.php
cd /usr/local/nginx
# 重置配置文件
\cp conf/nginx.conf.default conf/nginx.conf
# 开启动态页面配置
sed -i '65,68s/#//' conf/nginx.conf
sed -i '70,71s/#//' conf/nginx.conf
sed -i '70s/_params/.conf/' conf/nginx.conf
ss -antulp | grep nginx &> /dev/null
if [ `echo $?` -ne 0 ];then
user=`ss -antulp | grep 80 | awk -F\" 'NR==1{print $2}'`
killall $user &> /dev/null
sleep 1  # 防止进程没被终止完全 启服务报错
sbin/nginx
else
sbin/nginx -s reload #  重新加载配置文件
fi
sleep 1
echo -e "\033[32m==============================================\033[0m"
curl http://127.0.0.1/test.php
echo -e "\033[32m==============================================\033[0m"
echo -e "\033[32mnginx已经支持php静态页面！！\033[0m"
# 测试有数据库的动态页面
echo '<?php
$mysqli = new mysqli("localhost","root","","mysql");
if (mysqli_connect_errno()){
        die("Unable to connect!"). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
        printf("Host:%s",$row[0]);
        printf("</br>");
        printf("Name:%s",$row[1]);
        printf("</br>");
}
?>' > html/mysql.php
mysql -e "create user dcc@localhost identified by '123'" # 创建测试账户
curl http://127.0.0.1/mysql.php
echo ""
sleep 0.5
echo -e "\033[32m==============================================\033[0m"
echo -e "\033[32mnginx已经支持php动态页面！！\033[0m"
sleep 1
echo -e "\033[32mnginx已实现动静分离！！\033[0m"
#  注意 更好的效果请用浏览器访问  http://虚拟机IP/test.php  http://虚拟机IP/mysql.php
